package com.tajiang.business.setting.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.tajiang.business.R;
import com.tajiang.business.common.http.BaseResponse;
import com.tajiang.business.common.http.HttpHandler;
import com.tajiang.business.common.http.HttpResponseListener;
import com.tajiang.business.common.utils.LogUtils;
import com.tajiang.business.common.utils.SharedPreferencesUtils;
import com.tajiang.business.common.utils.ToastUtils;
import com.tajiang.business.common.utils.UserUtils;
import com.tajiang.business.common.widget.SwitchView;
import com.tajiang.business.model.User;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/8/23.
 */
public class SetBusinessStateDialog extends Dialog {

    public static final boolean OPEN_AUTO_PRINT = true;   //自动打印开启状态
    public static final boolean CLOSE_AUTO_PRINT = false;
    private boolean state;

    @BindView(R.id.tv_show_msg)
    TextView tvShowMsg;
    @BindView(R.id.btn_cancel)
    TextView btnCancel;
    @BindView(R.id.btn_ensure)
    TextView btnEnsure;

    private String strOpen = "开启";
    private String strClose = "关闭";

    SwitchView svPrint;
    UpdateStateListener updateStateListener;

    public void setUpdateStateListener(UpdateStateListener updateStateListener) {
        this.updateStateListener = updateStateListener;
    }

    public interface UpdateStateListener {
        public void updateState();
    }

    public SetBusinessStateDialog(Context context, SwitchView svPrint) {
        super(context, R.style.dialog_operate);
        this.svPrint = svPrint;
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_business_state_setting, null);
        setContentView(view);
        ButterKnife.bind(this);

        initView();
    }

    private void initView() {
        this.setCancelable(false);
        if (svPrint.isOpened() == OPEN_AUTO_PRINT){
            tvShowMsg.setText("关闭后用户不可在此档口下单\n\n确定关闭吗？");
            btnEnsure.setText(strClose);
            btnEnsure.setTextColor(getContext().getResources().getColor(R.color.red_orange));
        } else {
            tvShowMsg.setText("开启营业后用户可正常下单\n\n确定开启吗？");
            btnEnsure.setText(strOpen);
            btnEnsure.setTextColor(getContext().getResources().getColor(R.color.green_light));
        }
    }

    public void setTextView(String msg) {
        tvShowMsg.setText(msg);
    }

    @OnClick(R.id.btn_cancel)
    public void onCancelClick() {
        this.dismiss();
    }

    @OnClick(R.id.btn_ensure)
    public void onConfirmClick() {
//        svPrint.setOpened(!svPrint.isOpened());

        new HttpHandler(new HttpResponseListener() {
            @Override
            public void onSuccess(BaseResponse response) {
                if ((boolean)response.getData() == true) {

                    svPrint.setOpened(!svPrint.isOpened());

                    ToastUtils.showShort("档口" + (svPrint.isOpened() ? strOpen : strClose)  + "成功");

                    User user = UserUtils.getUser();
//                    user.setState(svPrint.isOpened()? User.STATE_OPEN : User.STATE_CLOSE);
                    SharedPreferencesUtils.put(SharedPreferencesUtils.USER_LOGIN_INFOR, user);
                } else {
                    ToastUtils.showShort("档口设置失败");
                }
            }

            @Override
            public void onFailed(BaseResponse response) {
                ToastUtils.showShort("档口设置失败");
            }

            @Override
            public void onFinish() {

            }
        }).updatStallsState(svPrint.isOpened()? User.STATE_CLOSE : User.STATE_OPEN);
        this.dismiss();
    }
}